//
//  BLPictureView.m
//  百思不得姐
//
//  Created by hbl on 16/3/21.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLPictureView.h"
#import "BLTopic.h"
#import "UIImageView+WebCache.h"
#import "M13ProgressViewRing.h"
#import "SDPhotoBrowser.h"
#import "BLBigImageViewController.h"
#import <YYWebImage.h>

@interface BLPictureView()
@property (weak, nonatomic) IBOutlet YYAnimatedImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIButton *moreButton;
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;

@property (weak, nonatomic) IBOutlet M13ProgressViewRing *progressView;

@end

@implementation BLPictureView
- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    self.moreButton.hidden = YES;
    self.progressView.secondaryColor = [UIColor whiteColor];
    self.progressView.primaryColor = [UIColor whiteColor];
    self.progressView.hidden = YES;
    [self.pictureView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPicture:)]];

}

- (void)onClickPicture:(UITapGestureRecognizer *)tap
{
    BLBigImageViewController *bigImage = [[BLBigImageViewController alloc]init];
    bigImage.topic = self.topic;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:bigImage animated:YES completion:nil];
}


+ (instancetype)pictureView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"BLPictureView" owner:nil options:nil] lastObject];
}

- (void)setTopic:(BLTopic *)topic
{
    _topic = topic;
    [self.progressView setProgress:0.0 animated:NO];
   
    [self.pictureView yy_setImageWithURL:[NSURL URLWithString:topic.image2] placeholder:nil options:YYWebImageOptionSetImageWithFadeAnimation progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressView.hidden = NO;
        CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize);
        [self.progressView setProgress:1.0 * progress animated:YES];
    } transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        [self.progressView setProgress:1.0 animated:YES];
        self.progressView.hidden = YES;
    }];
    
    self.gifImageView.hidden = ![self.topic.image2 hasSuffix:@"gif"];
    if (topic.picLong) {
        self.moreButton.hidden = NO;
        self.pictureView.contentMode = UIViewContentModeScaleAspectFill;
        self.pictureView.clipsToBounds = YES;
    }else{
        self.moreButton.hidden = YES;
        self.pictureView.contentMode = UIViewContentModeScaleToFill;
    }
}

@end
